﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFIDA.U9.SM.Ship;
using UFIDA.U9.PM.PO;
using UFSoft.UBF.PL;
using UFIDA.U9.PM.Rcv;
using UFIDA.U9.SM.RMA;
using UFIDA.U9.SM.SO;
using UFIDA.U9.CBO.SCM.Customer;
using UFIDA.U9.PR.PurchaseRequest;
using UFSoft.UBF.Util.DataAccess;
using UFSoft.UBF.PL.Engine;
using System.Data;
using UFIDA.U9.CBO.MFG.ProductionLine;


namespace UFIDA.U9.Cust.LS.PlugBE
{
    /// <summary>
    /// 爱发科
    /// 采购收货回写采购订单

    /// </summary>
    class AFKReceivementToPOExtend : IEventSubscriber
    {
        //配置XX.sub.xml文件放置到portal/bin下，dll文件放置到portal/ApplicationServer/Libs下
        public void Notify(params object[] args)
        {
            if (args != null && args.Length != 0 && args[0] is EntityEvent)
            {
                BusinessEntity.EntityKey key = ((EntityEvent)args[0]).EntityKey;
                UFIDA.U9.PM.Rcv.Receivement rcv = key.GetEntity() as UFIDA.U9.PM.Rcv.Receivement;
                if (rcv == null)
                    return;
                if (rcv.Org.Code.Contains("UCD")|| rcv.Org.Code.Contains("UCDH")) {
                    if (rcv.Status == RcvStatusEnum.Opened)
                     {
                        //保存时更新采购订单行数量自定义行14
                        foreach (var line in rcv.RcvLines)
                        {
                            Decimal ArriveQtyTBU = line.ArriveQtyTBU; // 实到数量
                            long lineid = line.SrcDoc.SrcDocLine.EntityID;// 采购订单行id
                            DataSet rcvsum;
                            String rcvsql = " SELECT line.SrcDoc_SrcDocLine_EntityID,sum(ArriveQtyTU)ArriveQtyTU from PM_RcvLine line   " +
                            "LEFT JOIN PM_Receivement rcv on rcv.id=line.Receivement" +
                            " LEFT JOIN Base_Organization org on org.id=rcv.Org" +
                            " WHERE rcv.Status=0 " +
                            "and org.Code  ='" + rcv.Org.Code + "'and line.ItemInfo_ItemID= '" + line.ItemInfo.ItemID.ID +
                            "'GROUP BY line.SrcDoc_SrcDocLine_EntityID,line.ItemInfo_ItemID,rcv.Org ";
                            DataAccessor.RunSQL(DataAccessor.GetConn(), rcvsql, null, out rcvsum);
                            if (rcvsum.Tables.Count > 0)
                            {
                                foreach (DataRow item in rcvsum.Tables[0].Rows)
                                {
                                    String sql = "Update PM_POLine set DescFlexSegments_PrivateDescSeg14=" + item["ArriveQtyTU"].ToString() + "   WHERE id='" + item["SrcDoc_SrcDocLine_EntityID"].ToString() + "'";
                                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null);
                                }
                            }
                        }

                    }
                  
                 }
                 
                }
            }
        }
     
}
